package com.surfilter.phantomjs.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringParseUtils {

    private static Logger logger = LoggerFactory.getLogger(StringParseUtils.class);

    private static String dzRegex = "[^：^:]*(省|市|区)[^：^\\s]*";

    private static String dhRegex = "([\\(|（]?\\s*([0-9]+(-|\\s)*)+[\\)|）]?)?\\s*([0-9]+(-|\\s)*)+";

    public static String parseDz(String dz){
        String result = "";
        try{
            if(dz != null)result = NumFormatUtils.parseString(dz, dzRegex, false);
        }catch(Exception e){
            e.printStackTrace();
            logger.error("匹配地址异常：" + dz , e);
        }
        return result;
    }

    public static String parseDh(String dh){
        String result = "";
        try{
            if(dh != null)result = parseString(dh, dhRegex, false, 5);
        }catch(Exception e){
            e.printStackTrace();
            logger.error("匹配电话异常：" + dh , e);
        }
        return result;
    }

    public static String parseString(String text, String regex, boolean regexAll, int length){
        StringBuffer strb = new StringBuffer();
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(text);
        while(match.find()){
            String value = match.group();
            if(value.length() >= length){
                strb.append(match.group());
                if(!regexAll)break;
            }
        }
        return strb.toString();
    }

    public static void main(String[] args) throws IOException {
        String encoding = "utf-8";
        String encoding1 = "gbk";
        Map<String, Integer> results = new HashMap<String, Integer>();
        String sourceFile = "e:/data/source/";
        String destFile = "e:/data/result.csv";
        List<String> resultstr = new ArrayList<String>();
        File[] files = new File(sourceFile).listFiles();
        for(File file : files){
            String str = null;
            if(file.getName().length() != 32){
                str = FileUtils.readFileToString(file, encoding1);
            }else{
                str = FileUtils.readFileToString(file, encoding);
            }

            Map<String, Integer> result = wordFrequencyCount(str, 2, 5);
            mergeMap(results, result);
        }
        for(String str : results.keySet()){
            String t_result = str + "," + results.get(str);
            System.out.println(t_result);
            resultstr.add(t_result);
        }
        FileUtils.writeLines(new File(destFile), encoding, resultstr);
    }

    public static void mergeMap(Map<String, Integer> map1, Map<String, Integer> map2){
        for(String key : map2.keySet()){
            if(map1.containsKey(key)){
                map1.put(key, map1.get(key) + map2.get(key));
            }else{
                map1.put(key, map2.get(key));
            }
        }
    }


    public static String parseString(String text, String regex, boolean regexAll){
        StringBuffer strb = new StringBuffer();
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(text);
        while(match.find()){
            strb.append(match.group());
            if(!regexAll)break;
        }
        return strb.toString();
    }

    public static List<String> parseStringL(String text, String regex, boolean regexAll){
        List<String> result = new ArrayList<String>();
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(text);
        while(match.find()){
            result.add(match.group());
            if(!regexAll)break;
        }
        return result;
    }

    public static int parseLength(String text, String regex){
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(text);
        int i = 0;
        while(match.find()){
            i ++;
        }
        return i;
    }

    public static String parseString(String text, String regex, int regexAll){
        String target = null;
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(text);
        int i = 0;
        while(true){
            boolean find = match.find();
            if(find){
                i ++;
                if(regexAll == i){
                    target = match.group();
                    break;
                }
            }else break;
        }
        return target;
    }

    /**
     * 词频统计
     */
    public static Map<String, Integer> wordFrequencyCount(String text, int minLength, int maxLength){
        Map<String, Integer> result = new HashMap<String, Integer>();
        //匹配中文正则
        String regex = "[\u4E00-\u9FA5]*";
        text = parseString(text, regex, true);
        if(text != null && !text.trim().equalsIgnoreCase("")){
            text = text.trim();
            if(text.length() >= minLength){
                for(int i = 0; i < text.length() - minLength; i ++){
                    if(text.length() - i >= minLength){
                        for(int j = minLength ; j <= ((text.length() - i) > maxLength ? maxLength : (text.length() - i)) ; j++){
                            String str = text.substring(i, i + j);
                            if(result.containsKey(str)){
                                result.put(str, result.get(str) + 1);
                            }else{
                                result.put(str, 1);
                            }
                        }
                    }else break;
                }
            }
        }
        return result;
    }

}
